package at.codebase.db.dao;

import static at.codebase.db.OfyService.ofy;

import java.util.List;

import at.codebase.db.entities.EApiAccess;
import at.codebase.db.entities.EApiPartner;

public class EApiAccessDao {
	
	public static void saveApiAccess(final EApiAccess apiAccess) {
		if(apiAccess.getAccessToken() != null && !apiAccess.getAccessToken().trim().equals(""))
		ofy().save().entity(apiAccess).now();
	}

	public static List<EApiAccess> getApiAccessList() {
		return ofy().load().type(EApiAccess.class).list();
	}

	public static EApiAccess getApiAccess(Long accessId) {
		if(accessId.longValue() <= 0)
			return null;
		return ofy().load().type(EApiAccess.class).filter("accessId", accessId).first().get();
	}
	
	public static boolean isValid(String accessToken){
		EApiAccess apiAccess = ofy().load().type(EApiAccess.class).filter("accessToken", accessToken).first().get();
		if(apiAccess == null)
			return false;
		return apiAccess.isActive();
	}
	
	public static EApiPartner getPartner(String accessToken){
		EApiAccess apiAccess = ofy().load().type(EApiAccess.class).filter("accessToken", accessToken).first().get();
		if(apiAccess == null)
			return null;
		if(apiAccess.getPartner() == null)
			return null;
		return apiAccess.getPartner().get();
	}
	
}
